package com.platform.enums;

import com.platform.comm.constants.EnumOption;
import com.platform.comm.enums.IDictEntry;
import com.platform.comm.enums.IEnumItem;
import com.platform.comm.enums.IEnumOption;
import lombok.Getter;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 元信息（字段）数据类型
 *
 * @author coder
 */
@EnumOption(name = "metadataDataType")
public enum MetadataDataType implements IDictEntry, IEnumItem, IEnumOption<String> {
    /**
     * 字符型
     */
    C("VARCHAR", "VARCHAR", "字符型"),
    CH("CHAR","CHAR","字符型"),
    /**
     * 数字型
     */
    N("DECIMAL", "NUMERIC", "数值型"),

    I("INTEGER","INTEGER","数值型"),


    /**
     * 日期型
     */
    D("DATE", "DATE", "日期型"),
    /**
     * 日期型
     */
    DT("DATE", "DATETIME", "日期时间型"),
    /**
     * 日期时间型
     */
    T("TIMESTAMP", "TIMESTAMP", "日期时间型"),
    /**
     * 日期时间型
     */
    TM("DATE", "TIME", "时间型"),
    /**
     * 备注型
     */
    M("STRING", "TEXT", "备注型");



    @Getter
    public final String typeName;
    @Getter
    public final String databaseType;
    @Getter
    public final String text;

    MetadataDataType(String typeName, String databaseType, String text) {
        this.typeName = typeName;
        this.databaseType = databaseType;
        this.text = text;
    }


    @Override
    public boolean is(String typeName) {
        return this.typeName.equalsIgnoreCase(typeName);
    }

    @Override
    public String getKey() {
        return this.typeName;
    }

    @Override
    public String getValue() {
        return this.getText();
    }

    @Override
    public Object getData() {
        return this;
    }

    public static MetadataDataType getByTypeName(String typeName) {
        return Stream.of(MetadataDataType.values()).filter(e -> e.getTypeName().equalsIgnoreCase(typeName)).findFirst().orElse(null);
    }

    public static MetadataDataType getByDataBaseType(String databaseType) {
        return Stream.of(MetadataDataType.values()).filter(e -> e.getDatabaseType().equalsIgnoreCase(databaseType)).findFirst().orElse(null);
    }

    /**
     * 枚举项 “键”
     *
     * @return key 键
     */
    @Override
    public String key() {
        return this.typeName;
    }

    /**
     * 枚举项 “值”
     *
     * @return value 值
     */
    @Override
    public String value() {
        return this.getText();
    }

    /**
     * 枚举options
     *
     * @return 枚举项组成的map
     */
    @Override
    public Map<String, String> enums() {
        return Arrays.stream(values()).collect(Collectors.toMap(IEnumOption::key, IEnumOption::value));
    }
}
